//gather counterfactual simulations by race and add together

//baseline data
local files `"baseline cfact_natsub cfact_locsub"'

foreach file in `files'{
	import delimited "$dir/Model/simulated_data/simulated_data_`file'_3.csv", clear
	gen v25 = 2
	tempfile blacks
	save `blacks'

	import delimited "$dir/Model/simulated_data/simulated_data_`file'_2.csv", clear
	gen v25 = 1
	append using `blacks'
	save "$temp/simulated_data_`file'_combined", replace
}


//read in and prepare files
local files `"baseline cfact_natsub cfact_locsub"'
foreach file in `files'{
    use "$temp/simulated_data_`file'_combined", clear
	ren v1 uniqid
	ren v2 year
	ren v3 mu
	ren v4 e
	ren v5 m
	ren v6 p
	ren v7 l
	ren v8 age
	ren v9 x
	ren v10 ac
	ren v11 f
	ren v12 lp_type
	ren v13 h
	ren v14 wage
	ren v15 l_prime
	ren v16 sim
	ren v17 age_int
	ren v21 tau
	ren v22 util
	ren v23 util_start
	ren v24 e_s
	ren v25 race
	//replace util_start = util_start/41
	
	ren util util_expost
	ren util_start util
	
	gen temp = l if age_int == 22 & e == 1
	replace temp = l if age_int == 25 & e==2
	bys uniqid sim: egen l_start = max(temp)
	//drop temp
	//keep if (age_int == 22 & e == 1 | age_int == 25 & e == 2)
	gen start_away = (l_start!=1)
	//drop if age_int<25	
	
	//various migration indicators
	gen parent = (l == 1)
	gen mig = (l_prime<11)

	//cost of living levels
	replace l = l - 1
	replace l = lp_type if l == 0
	gen coli = 0
	replace coli = 1.276 if l == 1
	replace coli = 1.212 if l == 2
	replace coli = 1.017 if l == 3
	replace coli = 1.002 if l == 4
	replace coli = 1.052 if l == 5
	replace coli = 0.970 if l == 6
	replace coli = 0.999 if l == 7
	replace coli = 1.064 if l == 8
	replace coli = 1.279 if l == 9	
	replace wage = wage/coli //now a real wage!
	replace wage = wage * 2.08 //reporting real wages in thousands of dollars now, since initially normalized by 2k
	replace util = util * 2.08 //same thing for utility
	replace util_expost = util_expost * 2.08 //same thing for ex-post utility

	//get: total experience, total wages and moves, proportion of life spent with parents. Also separate by college education to explore heterogeneity
	collapse (first) start_away mu race tau e lp_type (mean) util (max) x (sum) wage mig util_expost f parent, by(sim uniqid)
	
	ds x wage mig util util_expost f parent 
	foreach var in `r(varlist)'{
		ren `var' `var'_`file'
	}	
	save "$temp/lifecycle_collapsed_`file'", replace
}


//get to work
use "$temp/lifecycle_collapsed_baseline", clear
merge 1:1 uniqid sim using "$temp/lifecycle_collapsed_cfact_natsub", keep(match) nogen
merge 1:1 uniqid sim using "$temp/lifecycle_collapsed_cfact_locsub", keep(match) nogen

//women who actually have helpful grandparents
//keep if tau == 1

//compute various effects
local cfacts `"natsub locsub"'
local vars `"x wage mig util util_expost f parent"'

foreach cfact in `cfacts'{
	foreach var in `vars'{
			gen `var'_diff_`cfact' = `var'_cfact_`cfact' - `var'_baseline
	}
}

//tweak utility to be in dollars
replace util_diff_natsub = util_diff_natsub / 0.09
replace util_diff_locsub = util_diff_locsub / 0.09
replace util_expost_diff_natsub = util_expost_diff_natsub / 0.09
replace util_expost_diff_locsub = util_expost_diff_locsub / 0.09

//report summary statistics
capture program drop tableline 
program tableline
	local condition "`1'"
	local cfact "`2'"
	
	qui{
	preserve
		keep if `condition' 
		
		
		//summary stats: wages, x, moves, time with parents, fertility, and WTP
		su wage_diff_`cfact'
		local wage = round(`r(mean)', 0.01)
		
		su x_diff_`cfact'
		local x = round(`r(mean)', 0.01)
		
		su mig_diff_`cfact'
		local mig = round(`r(mean)', 0.01)
		
		su parent_diff_`cfact'
		local parent = round(`r(mean)', 0.01)
		
		su f_diff_`cfact'
		local f = round(`r(mean)', 0.01)
		
		su util_diff_`cfact'
		local util = round(`r(mean)', 0.01)
		
		su util_expost_diff_`cfact'
		local util_expost = round(`r(mean)', 0.01)
		noi di "`wage' & `x' & `mig' & `parent' & `f' & `util' & `util_expost' \\"
	restore
	}
end

cap log close
log using "$output/Table_13"
//produce the lines
local cfacts `"natsub locsub"'
foreach cfact in `cfacts'{
	noi di "Results for `cfact' Counterfactual:"
	tableline "uniqid>0" "`cfact'" //all
	tableline "tau == 1" "`cfact'" //helpful gpars
	tableline "tau == 2" "`cfact'" //unhelpful
	tableline "race == 1" "`cfact'" //whites
	tableline "race == 2" "`cfact'" //blacks
	tableline "e == 1" "`cfact'" //high school
	tableline "e == 2" "`cfact'" //college
	tableline "!start_away" "`cfact'" //start in lp
	noi di "" 
}
log close

//some useful summary stats
gen wage_diff_diff = wage_diff_natsub - wage_diff_locsub
gen util_diff_diff = util_diff_natsub - util_diff_locsub
gen util_expost_diff_diff = util_expost_diff_natsub - util_expost_diff_locsub


//demonstration of considerable right tails for folks who start in parent locadtion
su wage_diff_diff
su wage_diff_diff, d
su wage_diff_diff if !start_away, d //non-negliglble right tail here!
su util_diff_diff if !start_away, d
su util_expost_diff_diff if !start_away, d //non-negliglble right tail here!

***************Part 2: heterogeneity by grandparent location

//
capture program drop tableline_v2
program tableline_v2

	local condition "`1'"

	preserve
	qui{
		keep if `condition'
		
		//wages
		su wage_diff_natsub
		local wage1 = round(`r(mean)', 0.01)

		su wage_diff_locsub
		local wage2 = round(`r(mean)', 0.01)
		local diff1 = round(`wage1' - `wage2', 0.01)

		//ex-ante wtp
		su util_diff_natsub
		local util1 = round(`r(mean)', 0.01)

		su util_diff_locsub
		local util2 = round(`r(mean)', 0.01)
		local diff2 = round(`util1' - `util2', 0.01)
		
		//ex-post wtp
		su util_expost_diff_natsub
		local util_ep1 = round(`r(mean)', 0.01)

		su util_expost_diff_locsub
		local util_ep2 = round(`r(mean)', 0.01)
		local diff3 = round(`util_ep1' - `util_ep2', 0.01)
		
		
		//noi di "`wage1' & `wage2' & `diff1' & `util1' & `util2' & `diff2' & `util_ep1' & `util_ep2' & `diff3'  \\"
		noi di "`wage1' & `wage2' & `diff1' & `util_ep1' & `util_ep2' & `diff3'  \\"
	}
	restore
end

cap log close
log using "$output/Table_14"
//by location
forval i = 1/9{
	tableline_v2 "lp_type == `i'"
}
log close




//end of dofile